/**
 * Project  : Looker
 * Package  : net.freestu.looker.util
 * File     : NewsUtils.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : 2009-9-4
 * Modified : 2009-9-4
 * License  : Apache License 2.0 
 */
package net.freestu.looker.util;

import java.util.Iterator;
import java.util.regex.Pattern;

import net.freestu.looker.common.Looker;
import net.freestu.looker.common.Media;
import net.freestu.looker.common.NewsEntity;

/**
 *  
 */
public class NewsUtils
{
	  /**
	 * 替换掉新闻中的URL为本地URL
	 * 如./logo.gif 要替换为showMedia.php?mhash={mediaHash}
	 * 否则由于页面变化很多图片没法显示
	 */
	public static String  getOriginal(NewsEntity newsEntity)
	{
		Iterator<Media> it   = newsEntity.getMediaList().iterator();
		String      original = newsEntity.getContent();
		String mediaReplaced = Looker.gloalConfig.getString("looker.mediaReplaced");
		Media          media = null;
		while( it.hasNext())
		{
			media = it.next();
			//如果没出现错误，替换为本地ＵＲＬ
			original = original.replace(media.getUrl(), mediaReplaced.replace("{mediahash}", media.getHash()));	
		}
		return original;
	}
	
	/**
	 * 去掉需要不需要保留的标签
	 */
	public static String getBrief(NewsEntity newsEntity, String original)
	{
		//保留p,br,a,img三个标记，然后再去掉其他的所有标记
		//首先替换需保留的标记为的<>为[]
		String brief = original;

		brief = replaceString(brief,"<p.*?>(.*?)</p>", "[p]$1[/p]");
		brief = replaceString(brief,"<br.*?>", "[br /]");
		brief = replaceString(brief,"<img(.*?)>", "[img$1]");
		brief = replaceString(brief,"<a(.*?)>(.*?)</a>", "[a$1]$2[/a]");
		
		//去除其他标记
		brief = replaceString(brief,"<script(.[^(</script>)]*)</script>" , "");
		brief = replaceString(brief,"<.*?>", "");
		brief = replaceString(brief,"&nbsp;", " ");

		return brief;
	}
	
	/**
	 * 去除所有的HTML标记
	 */
	public static String getIntro(NewsEntity newsEntity)
	{
		return  HtmlUtils.stripTags(newsEntity.getContent());
	}
	
	/**
	 * 查找新闻缩略图
	 */
	public static String getThumb(NewsEntity newsEntity)
	{
		Iterator<Media> it   = newsEntity.getMediaList().iterator();
		Media media = null;
		StringBuffer buffer = new StringBuffer();
		boolean isFirst = true;
		while(it.hasNext()){
			media = it.next();
			
			if(media.getType().ordinal() != FileUtils.FileType.IMAGE.ordinal())
				continue;
			
			if(isFirst)
				isFirst = false;
			else {
				buffer.append("|");
			}
			buffer.append(media.getHash());
		}
		return buffer.toString();
	}
	
	/**
	 * 处理新闻附件类型
	 */
	public static int getImageCount(NewsEntity newsEntity)
	{	
		Iterator<Media> it   = newsEntity.getMediaList().iterator();
		int imgCnt		 = 0;
		Media media = null;
		while(it.hasNext()){
			media =it.next();
			if(media.getType().ordinal()==FileUtils.FileType.IMAGE.ordinal())
				imgCnt++;
		}
		return imgCnt;
	}
	
	/**
	 * 计算TAG
	 */
	public static String getTag()
	{
		//news.setTag(Tag.exactTag(news.getIntro()));
		return null;
	}
	
	/**
	 * 替换字符
	 * String.replaceAll()是大小写敏感的，这里重写了下ReplaceALL主要让大小写不敏感
	 */
	public static  String replaceString(String source,String pattern,String replace)
	{
		return Pattern.compile(pattern,Pattern.CASE_INSENSITIVE).matcher(source).replaceAll(replace);
	}
}
